/*
 * @Description: 按键模块-app层
 * @Author: yangsw
 * @Date: 2021-06-11 09:49:42
 * @LastEditTime: 2022-06-17 11:47:22
 * @LastEditors: yangsw
 * @Reference:
 */
#include "app_key.h"
#include "../../log/svc/svc_log.h"
#include "../../cloud/svc/svc_cloud_protocol.h"
#include "../../flash/svc/svc_flash.h"
#include "../../lcd/svc/svc_lcd.h"
#include "../../ir/svc/svc_ir.h"
#include "../../ir/svc/svc_ir_uart.h"
#include "../../nb/svc/svc_nb_core.h"
#include "../../pulse/svc/svc_pulse.h"
#include "../../cloud/svc/svc_cloud.h"
S_Key s_key;

/**
 * @brief: 按键模块线程
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void App_Key_Task(void)
{
  //  Svc_Log_Printf(LOG_INFO,"%s",__FUNCTION__);
  S_Lcd *s_lcd = Svc_Lcd_Get_Data();
  Bsp_Iwdg_Feed();
  switch (s_key.e_keyPressType)
  {
  case E_KEY_1S_STATE: // 切换界面
  {
    if (s_lcd->u8State == 0)
    {
      Svc_Lcd_Wakeup_Config();
    }
    else
    {
      Svc_Log_Printf(LOG_INFO, "->Svc_Lcd_Switch_Display");
      // 再切换界面
      Svc_Lcd_Switch_Display();
      // 显示时间置位
      Svc_Lcd_NoOpsCnt_Set();
    }
    s_key.e_keyPressType = E_KEY_INIT_STATE;
    break;
  }
  case E_KEY_CLAR_STATE:
  {
    if (Svc_Lcd_Is_Busy() == E_CHECK_OK)
    {
      Svc_Key_Clear_Lcd_Alarm();
    }
    s_key.e_keyPressType = E_KEY_INIT_STATE;
    break;
  }
  case E_KEY_3S_STATE: // 开启ir
  {
    Svc_Log_Printf(LOG_INFO, "->Svc_Ir_Uart_Wakeup_Init");
    Svc_Ir_Uart_Wakeup_Init();
    s_key.e_keyPressType = E_KEY_INIT_STATE;
    break;
  }
  case E_KEY_6S_STATE: // 开启up
  {
    if (s_key.key_up_cnt < 3)
    {
      Svc_Cloud_Post_Dev_Data_Event(E_CLOUD_DEV_DATA_EVENT_TOTAL_USAGE);
      Svc_Cloud_Post_Event(E_CLOUD_EVENT_DEVICE_INFO_UPLOAD);

      Svc_Cloud_Set_Upload_Reason(E_UR_Key);
      Svc_Lcd_Display_Upload_Reason(E_UR_Key, 6);
      //      Svc_Flash_Save_Meter_Event(E_Evevt_Up);

      s_key.key_up_cnt++;
      Svc_Log_Printf(LOG_INFO, "->E_KEY_OPEN_UPLOAD");
    }
    s_key.e_keyPressType = E_KEY_INIT_STATE;
    break;
  }
  case E_KEY_BREAK:
  {
    Svc_Lcd_Wakeup_Config();
    Svc_Cloud_Set_Upload_Reason(E_UR_Key_Abn);
    Svc_Alarm_Handler(Alarm_Key_Abn, E_Lcd_Key_Abn, E_Event_Key_Abn, E_UR_Key_Abn);
    s_key.e_keyPressType = E_KEY_INIT_STATE;
    break;
  }
  default:
    break;
  }
} 

/**
 * @brief: 获取按键模块相关信息结构体指针
 * @param  {void}
 * @return {S_Key*}  按键模块相关信息结构体指针
 * @note:
 * @see:
 */
S_Key *App_Key_Get_Data(void)
{
  return &s_key;
}

void App_Key_Up_Callback(void)
{
  if (s_key.key_up_cnt > 0)
  {
    s_key.key_up_time++;
  }
  if (s_key.key_up_time > 1800)
  {
    s_key.key_up_time = 0;
    s_key.key_up_cnt = 0;
  }
}

/**
 * @brief: 按键定时器回调
 * @param  {void}
 * @return {void}
 * @note: 每1s执行一次
 * @see:
 */
void App_Key_Timer_Callback(void)
{
  if (s_key.e_keyPressType != E_KEY_IS_PRESS)
  {
    s_key.key_ms_cnt = 0;
    return;
  }
  s_key.key_ms_cnt++;
  if (s_key.key_ms_cnt < 1000)
  {
    return;
  }
  s_key.key_ms_cnt = 0;
  if (Svc_Key_Get_State() == E_Gpio_Set)
  {
    Svc_Key_Press(&s_key);
  }
}

/**
 * @brief: 按键定时器回调
 * @param  {void}
 * @return {void}
 * @note: 每1ms执行一次
 * @see:
 */
void App_Key_Timer_Ms_Callback(void)
{
  if (s_key.e_keyPressType != E_KEY_IS_PRESS)
  {
    return;
  }
  if (Svc_Key_Get_State() == E_Gpio_Reset)
  { // 检测到一次按键松开
    s_key.key_rel_cnt++;
    if (s_key.key_rel_cnt == KEY_RELEASE)
    { // 确认按键松开
      Svc_Key_Release(&s_key);
      s_key.key_ms_cnt = 0;
    }
    if (s_key.key_rel_cnt > KEY_RELEASE)
    {
      s_key.key_rel_cnt = KEY_RELEASE;
    }
  }
  else
  {                        // 检测到一次按键按下
    s_key.key_rel_cnt = 0; // 松开计数清零
  }
  if (Svc_Key_Get_State() == E_Gpio_Set)
  {
    if (s_key.e_keyPressType == E_KEY_IS_PRESS)
    {
      s_key.key_set_cnt++;
    }
  }
}

/**
 * @brief: 按键模块初始化
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void App_Key_Init(void)
{
  Svc_Key_Init(&s_key);
}